﻿package mortal.game.scene3D.ai.ais
{
    import Message.Command.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import flash.events.*;
    import flash.utils.*;
    import frEngine.event.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.*;
    import mortal.game.rules.*;
    import mortal.game.scene3D.ai.base.*;
    import mortal.game.scene3D.events.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.view.common.cd.*;
    import mortal.game.view.skillProgress.*;
    import mortal.mvc.core.*;

    public class CollectAI extends AICommand
    {
        private var _monster:MonsterPlayer;
        private var _isCurCollectEnd:Boolean;
        private var _isProcessing:Boolean;
        private var _lastTime:int = 0;
        private var _timerId:int;
        private var _monsterCode:int;
        public static var collectingBoss:BossInfo = null;
        public static var entityId:SEntityId = null;

        public function CollectAI()
        {
            return;
        }// end function

        override public function start(param1:Function = null) : void
        {
            Log.error("采集start(onEnd:Function=null):void, time=" + getTimer());
            this._isProcessing = false;
            var _loc_2:* = FrameManager.flashFrame.getTime();
            super.start(param1);
            this._monster = data.target as MonsterPlayer;
            if (this._monster == null || this._monster.entityInfo == null || this._monster.entityInfo.entityInfo == null || _loc_2 - this._lastTime < 200)
            {
                Log.error("采集(now - _lastTime)=" + (_loc_2 - this._lastTime) + ", time=" + getTimer());
                this.stop();
                return;
            }
            this._lastTime = _loc_2;
            FrEventDispatcher.instance.proxyAddEventListener(data.meRole, PlayerEvent.ENTITY_DEAD, this.onEntityDead, false, 0, true);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCollectBegin, this.collectBeginHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCollectEnd, this.collectEndHandler);
            collectingBoss = this._monster.bossInfo;
            this._monsterCode = this._monster.bossInfo.code;
            entityId = this._monster.entityInfo.entityInfo.entityId;
            GameProxy.sceneProxy.collect(this._monster.entityInfo.entityInfo.entityId, true);
            Dispatcher.addEventListener(EventName.Scene_RemoveEntity, this.entityRemoved);
            if (this._timerId > 0)
            {
                clearTimeout(this._timerId);
            }
            if (!BossRule.isShareCollectBoss(this._monster))
            {
                this._timerId = setTimeout(this.onTimer, 15000);
            }
            return;
        }// end function

        protected function onEntityDead(event:Event) : void
        {
            this.stop();
            return;
        }// end function

        private function onTimer() : void
        {
            Log.error("采集超时， 停止CollectAI, 然后AutofightAI会自动开始新的采集AI, time=" + getTimer());
            this.stop();
            return;
        }// end function

        private function entityRemoved(event:DataEvent) : void
        {
            Log.error("采集entityRemoved， 停止CollectAI, time=" + getTimer());
            var _loc_2:* = event.data as SEntityId;
            if (entityId != null && EntityUtil.equal(entityId, _loc_2))
            {
                Log.error("采集entityRemoved， 停止CollectAI2, time=" + getTimer());
                this.stop();
            }
            return;
        }// end function

        private function onPlayerDeadHandler(event:Event) : void
        {
            Log.error("采集onPlayerDeadHandler, time=" + getTimer());
            this.stop();
            return;
        }// end function

        private function collectTimeOut() : void
        {
            Log.error("采集collectTimeOut, time=" + getTimer());
            this.stop();
            return;
        }// end function

        override public function stop(param1:Boolean = true) : void
        {
            Log.error("采集stop(isStopSendServerPoint:Boolean = true), time=" + getTimer());
            if (this._timerId > 0)
            {
                clearTimeout(this._timerId);
                this._timerId = -1;
            }
            collectingBoss = null;
            entityId = null;
            super.stop(param1);
            if (!this._isCurCollectEnd)
            {
                GameProxy.sceneProxy.stopCollect();
            }
            SkillProgressView.instance.updateCount(null, -1);
            FrEventDispatcher.instance.proxyRemoveEventListener(data.meRole, PlayerEvent.ENTITY_DEAD, this.onEntityDead, false);
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicCollectBegin, this.collectBeginHandler);
            NetDispatcher.removeCmdListener(EPublicCommand._ECmdPublicCollectEnd, this.collectEndHandler);
            RolePlayer.instance.entityInfo.removeEventListener(EntityInfoEventName.UpdateStatus, this.roleStatusUpdateHandler);
            Dispatcher.removeEventListener(EventName.Scene_RemoveEntity, this.entityRemoved);
            this._isCurCollectEnd = true;
            this._isProcessing = false;
            return;
        }// end function

        private function roleStatusUpdateHandler(event:EntityInfoEvent) : void
        {
            Log.error("采集roleStatusUpdateHandler, time=" + getTimer());
            if (RolePlayer.instance.entityInfo.entityInfo.status != EMoveStatus._EMoveStatusCollect)
            {
                this.stop();
            }
            return;
        }// end function

        private function collectBeginHandler(param1:Object) : void
        {
            this._isProcessing = true;
            Log.error("采集collectBeginHandler, time=" + getTimer());
            RolePlayer.instance.entityInfo.addEventListener(EntityInfoEventName.UpdateStatus, this.roleStatusUpdateHandler);
            if (BossRule.isShareCollectBoss(this._monster))
            {
                Log.error("采集isShareCollectBoss, time=" + getTimer());
                return;
            }
            SkillProgressView.instance.startByTotalTime(this._monster.entityInfo.entityInfo.mana * 1000, this.collectEnd);
            this._isCurCollectEnd = false;
            return;
        }// end function

        private function collectEnd() : void
        {
            Log.error("采集collectEnd, time=" + getTimer());
            if (this._monster.entityInfo == null || this._monster.entityInfo.entityInfo == null)
            {
                Log.error("采集_monster.entityInfo == null, time=" + getTimer());
                this._isCurCollectEnd = true;
                this._isProcessing = false;
                this.stop();
                return;
            }
            Cache.instance.skill.lastCollectFinishTime = getTimer();
            Cache.instance.skill.waittingServerCollectID = this._monster.entityID;
            GameProxy.sceneProxy.collect(this._monster.entityInfo.entityInfo.entityId, false);
            this._isCurCollectEnd = true;
            this._isProcessing = false;
            return;
        }// end function

        private function collectEndHandler(param1:Object) : void
        {
            Log.error("采集collectEndHandler, time=" + getTimer());
            SkillProgressView.instance.updateCount(null, -1);
            this._isCurCollectEnd = true;
            var _loc_2:* = Cache.instance.cd.registerCDData(CDDataType.publicCD, "skillPublicCD");
            if (_loc_2.isCoolDown)
            {
                return;
            }
            _loc_2.totalTime = 400;
            _loc_2.startCoolDown();
            Dispatcher.dispatchEvent(new DataEvent(EventName.WinePartyCollectEnd, this._monster.bossInfo));
            return;
        }// end function

        public static function get isWorking() : Boolean
        {
            return collectingBoss != null;
        }// end function

    }
}
